package com.hustzp.com.xichuangzhu.login;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.avos.avoscloud.AVException;
import com.avos.avoscloud.AVOSCloud;
import com.avos.avoscloud.AVUser;
import com.avos.avoscloud.RequestMobileCodeCallback;
import com.hustzp.com.xichuangzhu.R;
import com.hustzp.com.xichuangzhu.XCZBaseFragmentActivity;
import com.hustzp.com.xichuangzhu.utils.Constant;
import com.hustzp.com.xichuangzhu.utils.DialogFactory;
import com.hustzp.com.xichuangzhu.utils.PhoneNumberTools;

import org.json.JSONException;
import org.json.JSONObject;

public class ReceiveSmsCodeActivity extends XCZBaseFragmentActivity {

    private EditText phoneNumEt;
    private Button sendBtn;

    private String intentAction;
    public static String phoneNumber = "";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_send_sms_code);
        intentAction = getIntent().getAction();

        initToolbar();
        initView();
    }

    private void initToolbar() {
        // 上级标题
        TextView navTxt = (TextView) findViewById(R.id.tv_nav_txt);
        if (Constant.INTENT_ACTION_REGISTER.equals(intentAction)) {
            navTxt.setText(getString(R.string.login));
        } else if (Constant.INTENT_ACTION_RESTPASW.equals(intentAction)) {
            navTxt.setText(getString(R.string.login));
        } else if (Constant.INTENT_ACTION_CHGPHONE.equals(intentAction)) {
            navTxt.setText(getString(R.string.account_and_passwd));
        }


        TextView titleTxt = (TextView) findViewById(R.id.tv_title_txt);
        if (Constant.INTENT_ACTION_REGISTER.equals(intentAction)) {
            titleTxt.setText(getString(R.string.register));
        } else if (Constant.INTENT_ACTION_RESTPASW.equals(intentAction)) {
            titleTxt.setText(getString(R.string.forget_password_account));
        } else if (Constant.INTENT_ACTION_CHGPHONE.equals(intentAction)) {
            titleTxt.setText(getString(R.string.acount_change_phonenumber));
        }
    }

    public void initView() {
        phoneNumEt = (EditText) findViewById(R.id.tv_phone_number);
        sendBtn = (Button) findViewById(R.id.send_code_btn);
        sendBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                final String phoneNum = phoneNumEt.getText().toString().trim();
                 phoneNumber= phoneNum;

                if (TextUtils.isEmpty(phoneNum)) {
                    DialogFactory.hintDialog(ReceiveSmsCodeActivity.this, getString(R.string.phone_number_not_empity));
                    return;
                }

                if (!PhoneNumberTools.verifyPhoneNumber(phoneNum)) {
                    DialogFactory.hintDialog(ReceiveSmsCodeActivity.this, getResources().getString(R.string.mainland_phonenumber));
                    return;
                }

                AVUser avUser = AVUser.getCurrentUser();
                if( avUser != null){

                    String currentPhoneNumber = avUser.getMobilePhoneNumber();
                    phoneNumber= currentPhoneNumber;

                    boolean isHaved = currentPhoneNumber.equals(ReceiveSmsCodeActivity.phoneNumber);
                    boolean  isVerify =  AVUser.getCurrentUser().isMobilePhoneVerified();
                    if(isHaved && isVerify){

                        DialogFactory.hintDialog(ReceiveSmsCodeActivity.this, getResources().getString(R.string.number_is_registed));

                        return;
                    }

                }else{

                }

//
//                AVUser.requestLoginSmsCodeInBackground("13577778888", new RequestMobileCodeCallback() {
//                    @Override
//                    public void done(AVException e) {
//
//                        if(null != e){
//
//                            DialogFactory.hintDialog(ReceiveSmsCodeActivity.this, e.getMessage());
//
//                        }
//
//                    }
//                });

                AVOSCloud.requestSMSCodeInBackground(phoneNum,new RequestMobileCodeCallback(){

                    @Override
                    public void done(AVException e) {

                        phoneNumber = phoneNum;

                        if(null != e){

                            String message = e.getMessage();
                            try {
                                JSONObject messageObject = new JSONObject(message);
                                String code = messageObject.getString("code");
                                if("601".equals(code)){

                                    DialogFactory.hintDialog(ReceiveSmsCodeActivity.this, getResources().getString(R.string.acount_one_phonenumber_fail_wait));

                                }
                            } catch (JSONException e1) {
                                e1.printStackTrace();
                            }
                        }else{
                            Intent intent = new Intent(ReceiveSmsCodeActivity.this, SmsCodeVerifyActivity.class);
                            intent.setAction(intentAction);
                            startActivity(intent);
                            Toast.makeText(ReceiveSmsCodeActivity.this,getResources().getText(R.string.acount_one_phonenumber_resent),Toast.LENGTH_SHORT).show();
                        }
                    }
                });
//                AVOSCloud.requestLoginSmsCodeInBackground(phoneNum, new RequestMobileCodeCallback() {
//                    @Override
//                    public void done(AVException e) {
//
//                    }
//                });

//                // 发送验证码
//                AVOSCloud.requestSMSCodeInBackground(phoneNum, new RequestMobileCodeCallback() {
//                    @Override
//                    public void done(AVException e) {
//                        if (e == null) {
//                            if (Constant.INTENT_ACTION_REGISTER.equals(intentAction)) {
//                                // 注册存储手机号码，到注册最后一步用于注册信息
////                                user.setMobilePhoneNumber(phoneNum);
//                                phoneNumber = phoneNum;
//                            }
//                            Intent intent = new Intent(ReceiveSmsCodeActivity.this, SmsCodeVerifyActivity.class);
//                            intent.setAction(intentAction);
//                            startActivity(intent);
//                        } else {
//
//                            String message = e.getMessage();
//                            try {
//                                JSONObject messageObject = new JSONObject(message);
//                                String code = messageObject.getString("code");
//                                if("601".equals(code)){
//                                    DialogFactory.hintDialog(ReceiveSmsCodeActivity.this,"今天短信已经超出限制");
//                                }else {
//
//                                DialogFactory.hintDialog(ReceiveSmsCodeActivity.this, getResources().getString(R.string.sms_send_error));
//                                }
//                            } catch (JSONException e1) {
//                                e1.printStackTrace();
//                            }
//
//                        }
//                    }
//                });
            }
        });
    }

    public void closeView(View view){
        finish();
    }

}
